package com.demo.amusic;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

/**
 * 使用前台服务开始播放音频
 */
public class PlayerService extends Service {

    private static final String ACTION_START = "ACTION_START";
    private static String ACTION_PREVIOUS = "ACTION_PREVIOUS";
    private static String ACTION_NEXT = "ACTION_NEXT";
    private static String ACTION_GONE = "ACTION_GONE";
    private static String ACTION_STOP = "ACTION_STOP";
    private NotificationReceiver notificationReceiver;


    public static void startPlayerService() {
        Intent intent = new Intent(MyApplication.getContext(), PlayerService.class);
        intent.setAction(ACTION_START);
        MyApplication.getContext().startService(intent);
    }

    public static void playerPreviousService() {
        Intent intent = new Intent(MyApplication.getContext(), PlayerService.class);
        intent.setAction(ACTION_PREVIOUS);
        MyApplication.getContext().startService(intent);
    }
    public static void playerNextService() {
        Intent intent = new Intent(MyApplication.getContext(), PlayerService.class);
        intent.setAction(ACTION_NEXT);
        MyApplication.getContext().startService(intent);
    }

    public static void stopPlayerService() {
        Intent intent = new Intent(MyApplication.getContext(), PlayerService.class);
        MyApplication.getContext().stopService(intent);
    }

    @Override
    public void onCreate() {
        Log.e("PlayService","onCreate");
        super.onCreate();
        // 开始前台服务
        Notifier.getInstance().init(this);
        notificationReceiver=new NotificationReceiver();
        //注册通知栏广播接收器
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(NotificationReceiver.ACTION_STATUS_BAR);
        registerReceiver(notificationReceiver,intentFilter);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("PlayService","onBind");
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("PlayService","onStartCommand");
        if (intent != null && intent.getAction() != null) {
            if (ACTION_START.equals(intent.getAction())) {
                AudioPlayer.getInstance().play ();
            } else if (ACTION_PREVIOUS.equals(intent.getAction())) {
                AudioPlayer.getInstance().previous();
            } else if (ACTION_NEXT.equals(intent.getAction())) {
                AudioPlayer.getInstance().next();
            }
        }
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.e("PlayService","onDestroy");
        super.onDestroy();
        AudioPlayer.getInstance().release();
        Notifier.getInstance().stopForeground();
    }
}
